using DocumentCreationSystem.Models.AgentOps;

namespace DocumentCreationSystem.Services.AgentOps
{
    /// <summary>
    /// Agentic对话增强器接口 - 为对话窗口提供工作流监控和智能决策能力
    /// </summary>
    public interface IAgenticDialogEnhancer
    {
        /// <summary>
        /// 初始化对话增强器
        /// </summary>
        /// <param name="dialogContext">对话上下文</param>
        Task InitializeAsync(DialogContext dialogContext);

        /// <summary>
        /// 处理用户消息并提供智能建议
        /// </summary>
        /// <param name="userMessage">用户消息</param>
        /// <param name="context">对话上下文</param>
        /// <returns>增强的响应</returns>
        Task<DocumentCreationSystem.Models.AgentOps.EnhancedDialogResponse> ProcessUserMessageAsync(string userMessage, DialogContext context);

        /// <summary>
        /// 监控工作流状态变化
        /// </summary>
        /// <param name="workflowId">工作流ID</param>
        /// <returns>状态监控信息</returns>
        Task<WorkflowStatusMonitor> MonitorWorkflowStatusAsync(string workflowId);

        /// <summary>
        /// 提供智能决策建议
        /// </summary>
        /// <param name="decisionContext">决策上下文</param>
        /// <returns>决策建议</returns>
        Task<SmartDecisionSuggestion> GetSmartDecisionAsync(DecisionContext decisionContext);

        /// <summary>
        /// 生成工作流进度报告
        /// </summary>
        /// <param name="workflowId">工作流ID</param>
        /// <returns>进度报告</returns>
        Task<WorkflowProgressReport> GenerateProgressReportAsync(string workflowId);

        /// <summary>
        /// 检测用户意图并推荐操作
        /// </summary>
        /// <param name="userInput">用户输入</param>
        /// <param name="context">上下文</param>
        /// <returns>意图分析和操作推荐</returns>
        Task<IntentAnalysisResult> AnalyzeIntentAndRecommendActionsAsync(string userInput, DialogContext context);

        /// <summary>
        /// 获取上下文感知的帮助信息
        /// </summary>
        /// <param name="context">当前上下文</param>
        /// <returns>上下文相关的帮助信息</returns>
        Task<ContextualHelpInfo> GetContextualHelpAsync(DialogContext context);

        /// <summary>
        /// 预测用户下一步可能的操作
        /// </summary>
        /// <param name="context">当前上下文</param>
        /// <returns>预测的操作列表</returns>
        Task<List<PredictedAction>> PredictNextActionsAsync(DialogContext context);

        /// <summary>
        /// 注册对话事件监听器
        /// </summary>
        /// <param name="listener">事件监听器</param>
        void RegisterEventListener(IDialogEventListener listener);

        /// <summary>
        /// 移除对话事件监听器
        /// </summary>
        /// <param name="listener">事件监听器</param>
        void RemoveEventListener(IDialogEventListener listener);
    }

    /// <summary>
    /// 对话事件监听器接口
    /// </summary>
    public interface IDialogEventListener
    {
        /// <summary>
        /// 用户消息事件
        /// </summary>
        Task OnUserMessageAsync(string message, DialogContext context);

        /// <summary>
        /// AI响应事件
        /// </summary>
        Task OnAIResponseAsync(string response, DialogContext context);

        /// <summary>
        /// 工作流状态变更事件
        /// </summary>
        Task OnWorkflowStatusChangedAsync(string workflowId, WorkflowStatus oldStatus, WorkflowStatus newStatus);

        /// <summary>
        /// 决策请求事件
        /// </summary>
        Task OnDecisionRequestedAsync(DecisionContext decisionContext);
    }



    /// <summary>
    /// 对话消息
    /// </summary>
    public class DialogMessage
    {
        /// <summary>
        /// 消息ID
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 发送者角色
        /// </summary>
        public MessageRole Role { get; set; }

        /// <summary>
        /// 消息内容
        /// </summary>
        public string Content { get; set; } = string.Empty;

        /// <summary>
        /// 时间戳
        /// </summary>
        public DateTime Timestamp { get; set; } = DateTime.Now;

        /// <summary>
        /// 消息类型
        /// </summary>
        public MessageType Type { get; set; } = MessageType.Text;

        /// <summary>
        /// 附加数据
        /// </summary>
        public Dictionary<string, object> Metadata { get; set; } = new();
    }

    /// <summary>
    /// 消息角色枚举
    /// </summary>
    public enum MessageRole
    {
        User,
        Assistant,
        System,
        Tool
    }

    /// <summary>
    /// 消息类型枚举
    /// </summary>
    public enum MessageType
    {
        Text,
        Command,
        Notification,
        Progress,
        Error,
        Warning,
        Success
    }

    /// <summary>
    /// 对话状态枚举
    /// </summary>
    public enum DialogState
    {
        /// <summary>
        /// 就绪状态
        /// </summary>
        Ready,

        /// <summary>
        /// 处理中
        /// </summary>
        Processing,

        /// <summary>
        /// 等待用户输入
        /// </summary>
        WaitingForInput,

        /// <summary>
        /// 工作流执行中
        /// </summary>
        WorkflowRunning,

        /// <summary>
        /// 错误状态
        /// </summary>
        Error,

        /// <summary>
        /// 暂停状态
        /// </summary>
        Paused
    }

    /// <summary>
    /// 用户偏好设置
    /// </summary>
    public class UserPreferences
    {
        /// <summary>
        /// 详细程度级别
        /// </summary>
        public VerbosityLevel VerbosityLevel { get; set; } = VerbosityLevel.Normal;

        /// <summary>
        /// 是否启用自动建议
        /// </summary>
        public bool EnableAutoSuggestions { get; set; } = true;

        /// <summary>
        /// 是否启用进度通知
        /// </summary>
        public bool EnableProgressNotifications { get; set; } = true;

        /// <summary>
        /// 首选的响应格式
        /// </summary>
        public ResponseFormat PreferredResponseFormat { get; set; } = ResponseFormat.Markdown;

        /// <summary>
        /// 自定义设置
        /// </summary>
        public Dictionary<string, object> CustomSettings { get; set; } = new();
    }

    /// <summary>
    /// 详细程度级别枚举
    /// </summary>
    public enum VerbosityLevel
    {
        Minimal,
        Normal,
        Detailed,
        Verbose
    }

    /// <summary>
    /// 响应格式枚举
    /// </summary>
    public enum ResponseFormat
    {
        Text,
        Markdown,
        HTML,
        JSON
    }

    /// <summary>
    /// 增强的对话响应
    /// </summary>
    public class EnhancedDialogResponse
    {
        /// <summary>
        /// 主要响应内容
        /// </summary>
        public string MainResponse { get; set; } = string.Empty;

        /// <summary>
        /// 智能建议列表
        /// </summary>
        public List<SmartSuggestion> Suggestions { get; set; } = new();

        /// <summary>
        /// 工作流状态更新
        /// </summary>
        public List<WorkflowStatusUpdate> WorkflowUpdates { get; set; } = new();

        /// <summary>
        /// 上下文操作
        /// </summary>
        public List<ContextAction> ContextActions { get; set; } = new();

        /// <summary>
        /// 响应元数据
        /// </summary>
        public ResponseMetadata Metadata { get; set; } = new();

        /// <summary>
        /// 是否需要用户确认
        /// </summary>
        public bool RequiresConfirmation { get; set; } = false;

        /// <summary>
        /// 确认消息
        /// </summary>
        public string? ConfirmationMessage { get; set; }
    }

    /// <summary>
    /// 智能建议
    /// </summary>
    public class SmartSuggestion
    {
        /// <summary>
        /// 建议ID
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 建议标题
        /// </summary>
        public string Title { get; set; } = string.Empty;

        /// <summary>
        /// 建议描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 建议类型
        /// </summary>
        public SuggestionType Type { get; set; }

        /// <summary>
        /// 置信度 (0-1)
        /// </summary>
        public double Confidence { get; set; }

        /// <summary>
        /// 优先级
        /// </summary>
        public SuggestionPriority Priority { get; set; } = SuggestionPriority.Normal;

        /// <summary>
        /// 执行动作
        /// </summary>
        public string? Action { get; set; }

        /// <summary>
        /// 动作参数
        /// </summary>
        public Dictionary<string, object> ActionParameters { get; set; } = new();
    }

    /// <summary>
    /// 建议类型枚举
    /// </summary>
    public enum SuggestionType
    {
        /// <summary>
        /// 工作流操作
        /// </summary>
        WorkflowAction,

        /// <summary>
        /// 内容改进
        /// </summary>
        ContentImprovement,

        /// <summary>
        /// 效率优化
        /// </summary>
        EfficiencyOptimization,

        /// <summary>
        /// 质量提升
        /// </summary>
        QualityEnhancement,

        /// <summary>
        /// 学习建议
        /// </summary>
        LearningTip,

        /// <summary>
        /// 故障排除
        /// </summary>
        Troubleshooting
    }

    /// <summary>
    /// 建议优先级枚举
    /// </summary>
    public enum SuggestionPriority
    {
        Low,
        Normal,
        High,
        Critical
    }

    /// <summary>
    /// 工作流状态更新
    /// </summary>
    public class WorkflowStatusUpdate
    {
        /// <summary>
        /// 工作流ID
        /// </summary>
        public string WorkflowId { get; set; } = string.Empty;

        /// <summary>
        /// 工作流名称
        /// </summary>
        public string WorkflowName { get; set; } = string.Empty;

        /// <summary>
        /// 旧状态
        /// </summary>
        public WorkflowStatus OldStatus { get; set; }

        /// <summary>
        /// 新状态
        /// </summary>
        public WorkflowStatus NewStatus { get; set; }

        /// <summary>
        /// 进度百分比
        /// </summary>
        public int ProgressPercentage { get; set; }

        /// <summary>
        /// 状态消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdateTime { get; set; } = DateTime.Now;
    }

    /// <summary>
    /// 上下文操作
    /// </summary>
    public class ContextAction
    {
        /// <summary>
        /// 操作ID
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 操作名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 操作描述
        /// </summary>
        public string Description { get; set; } = string.Empty;

        /// <summary>
        /// 操作类型
        /// </summary>
        public ActionType Type { get; set; }

        /// <summary>
        /// 是否自动执行
        /// </summary>
        public bool AutoExecute { get; set; } = false;

        /// <summary>
        /// 执行命令
        /// </summary>
        public string? Command { get; set; }

        /// <summary>
        /// 命令参数
        /// </summary>
        public Dictionary<string, object> Parameters { get; set; } = new();
    }

    /// <summary>
    /// 操作类型枚举
    /// </summary>
    public enum ActionType
    {
        /// <summary>
        /// 启动工作流
        /// </summary>
        StartWorkflow,

        /// <summary>
        /// 暂停工作流
        /// </summary>
        PauseWorkflow,

        /// <summary>
        /// 恢复工作流
        /// </summary>
        ResumeWorkflow,

        /// <summary>
        /// 停止工作流
        /// </summary>
        StopWorkflow,

        /// <summary>
        /// 创建文件
        /// </summary>
        CreateFile,

        /// <summary>
        /// 打开文件
        /// </summary>
        OpenFile,

        /// <summary>
        /// 运行工具
        /// </summary>
        RunTool,

        /// <summary>
        /// 显示信息
        /// </summary>
        ShowInfo,

        /// <summary>
        /// 导航到页面
        /// </summary>
        Navigate
    }

    /// <summary>
    /// 响应元数据
    /// </summary>
    public class ResponseMetadata
    {
        /// <summary>
        /// 处理时间（毫秒）
        /// </summary>
        public long ProcessingTimeMs { get; set; }

        /// <summary>
        /// 使用的AI模型
        /// </summary>
        public string? AIModel { get; set; }

        /// <summary>
        /// 置信度评分
        /// </summary>
        public double ConfidenceScore { get; set; }

        /// <summary>
        /// 相关工作流
        /// </summary>
        public List<string> RelatedWorkflows { get; set; } = new();

        /// <summary>
        /// 触发的事件
        /// </summary>
        public List<string> TriggeredEvents { get; set; } = new();

        /// <summary>
        /// 额外信息
        /// </summary>
        public Dictionary<string, object> AdditionalInfo { get; set; } = new();
    }
}
